package com.jc.oms.controller.newshopapi;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.oms.controller.BaseController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SuppressWarnings("all")
@Controller
@RequestMapping("newShopApi")
public class NewShopApiController extends BaseController {


    @Value("${new.shop.url}")
    private String newShopUrl;


//    @RequestMapping(value = "/initiateRequest" , method = RequestMethod.POST)
//    public Map<String,Object> initiateRequest(HttpServletRequest request){
//        Map<String,Object> resultMap = new HashMap<String, Object>(){{
//            put("code",10000);
//        }};
//        Map<String,Object> paramMap = new HashMap<>();
//        try{
//            paramMap = WebUtils.ReadAsChars(request);
//            if (paramMap.get("method") == null)throw new BaseServiceException("必传参数为空! method");
//            //发起请求
//            HttpUtil.post(newShopUrl+paramMap.get("method").toString(),paramMap);
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//        }catch (Exception e){
//
//        }
//        return null;
//    }

    String queryProductAttributes = "[{'id':34,'propertyCode':'test','propertyName':'测试','propertyStatusCH':'启用','categoryName':'分类测试1','propertyValues':'测试1,测试2'}]";

    String queryProductAttrAndValInfo = "{'id':34,'propertyCode':'test','propertyName':'测试','propertyStatusCH':'启用','propertyStatus':'0','categoryName':'分类测试1','propertyValues':[{'id':12,'value':'测试1'},{'id':334,'value':'测试2'}]}";


    @RequestMapping(value = "/initiateRequest" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> initiateRequest(HttpServletRequest request){
        Map<String,Object> resultMap = new HashMap<String, Object>(){{
            put("code",10000);
        }};
        Map<String,Object> paramMap = new HashMap<>();
        try{


            paramMap = WebUtils.ReadAsChars(request);
            if (paramMap.get("method") == null)throw new BaseServiceException("必传参数为空! method");
            //发起请求
            switch(paramMap.get("method").toString()) {
                case "queryProductAttributes":
                    resultMap.put("data", JSONObject.parseObject(queryProductAttributes, List.class));
                    break;
                case "queryProductAttrAndValInfo":
                    resultMap.put("data",JSONObject.parseObject(queryProductAttrAndValInfo,Map.class));
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }






}
